import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/hot",
    //路由的重定向
  },
  {
    path: "/index",
    name: "index",
    component: () => import("../views/Index.vue"),
    children: [
      {
        path: "/movie",
        name: "movie",
        component: () => import("../views/movie/Index.vue"),
        children: [
          {
            path: "/hot",
            name: "hot",
            component: () => import("../views/movie/Hot.vue"),
          },
          {
            path: "/thot",
            name: "thot",
            component: () => import("../views/movie/Thot.vue"),
          },
        ],
      },
      {
        path: "/cinema",
        name: "cinema",
        component: () => import("../views/Cinema.vue"),
        beforeEnter(to, from, next) {
          console.log(to, from);
          // next();
          if (localStorage.getItem("user")) {
            next();
          } else {
            next("/login");
          }
        },
      },
    ],
  },
  {
    path: "/my",
    name: "my",
    component: () => import("../views/My.vue"),
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../views/Login.vue"),
  },
];
// 个人中心没有底部导航，影院和热映都有底部导航

const router = new VueRouter({
  routes,
});

// router.beforeEach((to, from, next) => {
//   // console.log(to);
//   // console.log(from);
//   // next();

//   if (localStorage.getItem("user")) {
//     next();
//   } else {
//     // next("/login");
//     //直接next login 会死循环 因为 全局守卫也会守卫login页面
//     if (to.path == "/login") {
//       next();
//     } else {
//       next("/login");
//     }
//     //避免死循环 如果是login就继续 不是login才跳转到login
//   }
//   //如果本地存储中有user证明已经登陆成功了 那么就可以正常访问，如果没有登陆 成功那么 就跳转到登陆页面
// });
//路由跳转之前
// 导航守卫的回调函数中 有三个参数 分别是 to到哪儿去 from 从哪儿来 next执行下一步

// router.afterEach((to, from) => {
//   console.log(from, to);
// });
//路由跳转之后 执行的钩子函数 并没有next参数

export default router;
